package com.stockz.equity.charting.view;

import java.awt.BorderLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import com.stockz.downloader.DownloadManager;
import com.stockz.equity.charting.builder.StockChartViewBuilder;
import com.stockz.equity.manager.BarManager;
import com.stockz.equity.manager.ScripManager;
import com.stockz.equity.model.Bar;
import com.stockz.equity.model.Scrip;
import com.stockz.equity.pattern.View;

@Component
@Lazy(false)
public class StockChartView extends JPanel implements View,Ordered,InitializingBean {
	private static final long serialVersionUID = 748852749254325290L;

	private StockChart selectedStockChart;
	private List<StockChart> stockChartList = new ArrayList<StockChart>();
	
	@Autowired private BarManager barManager;
	@Autowired private ScripManager scripManager;
	@Autowired private StockChart defaultStockChart;
	@Autowired private StockChartViewBuilder stockChartViewBuilder;
	@Autowired private DownloadManager downloadManager;

	@Autowired private StockChartViewControlPanel stockChartViewControlPanel;
	@Autowired private StockChartViewSkin stockChartViewSkin;
	@Autowired private StockChartViewStructure stockChartViewStructure;
	
	public StockChartView() {
		setName("Chart");
		setBorder(null);
		setLayout(new BorderLayout());

		addComponentListener(new ComponentAdapter(){
			@Override
			public void componentResized(ComponentEvent e) {
				validate();
			}
		});
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		if(stockChartList.isEmpty()){
			List<Scrip> scripList = scripManager.getAllScripList(downloadManager.getLastDownloadDate());
			if(scripList != null && !scripList.isEmpty()){
				Scrip scrip = scripList.get(0);
				List<Bar> barList = barManager.getBarListByScripCode(scrip.getScripCode());
				StockChartPanelState state = defaultStockChart.getStockChartPanel().getStockChartPanelState();
				state.setScrip(scrip);
				state.getBarList().clear();
				state.getBarList().addAll(barList);
				state.getBackupBarList().clear();
				state.getBackupBarList().addAll(barList);
			}
			stockChartList.add(defaultStockChart);
			setSelectedStockChart(defaultStockChart);
		}
	}
	
	@Override
	public int getOrder() {
		return 1;
	}

	@Override
	public void validate() {
		super.validate();
		stockChartViewBuilder.build();
	}
	

	public StockChart getSelectedStockChart() {
		return selectedStockChart;
	}

	public void setSelectedStockChart(StockChart selectedStockChart) {
		this.selectedStockChart = selectedStockChart;
	};

	public List<StockChart> getStockChartList() {
		return stockChartList;
	}

	public StockChartViewControlPanel getStockChartControlPanel() {
		return stockChartViewControlPanel;
	}

	public StockChartViewSkin getStockChartViewSkin() {
		return stockChartViewSkin;
	}

	public StockChartViewStructure getStockChartViewStructure() {
		return stockChartViewStructure;
	}
}
